package com.chennan.book.domain.service;

import com.chennan.book.domain.model.Book;
import com.chennan.book.domain.model.FInvoiceApplication;
import com.chennan.book.domain.model.FaDataMapping;
import com.chennan.book.domain.repository.BookMapper;
import com.chennan.springaop.domain.annotation.PreventRepeatedSubmit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chennan
 * @createTime 2021年07月23日 14:09:00
 */
@Service
public class BookService implements BookServiceInteface{

    @Autowired
    private BookMapper book;
    @Autowired
    private BookServiceInteface bookService;

    @Override
    @PreventRepeatedSubmit(lockTime=10)
    public String getSubmit(List<String> list){

        return list.toString();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMybatisCacheHave(String no){
        book.updateInvoiceByInvoiceApplicationNo(no);
    }

    @Override
    public FaDataMapping queryFADATAMAPPING(String s) {
        return book.queryFADATAMAPPING(s);
    }

    @Override
    public List<FaDataMapping> queryFADATAMAPPINGs() {
        return book.queryFADATAMAPPINGs();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testMybatisCacheHave(String no){
        FInvoiceApplication fInvoiceApplication = book.queryInvoiceByInvoiceApplicationNo(no);
        queryNext(fInvoiceApplication, no, "有事务");
    }

    @Override
    public void testMybatisCacheNot(String no) {
        FInvoiceApplication fInvoiceApplication = book.queryInvoiceByInvoiceApplicationNo(no);
        queryNext(fInvoiceApplication,no,"无事务");
    }

    private void queryNext(FInvoiceApplication fInvoiceApplication, String no, String s) {
        FInvoiceApplication fInvoiceApplication1 = book.queryInvoiceByInvoiceApplicationNo(no);
        System.out.println(s+"两次查询是否是同一个对象："+(fInvoiceApplication==fInvoiceApplication1));
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public void testRetryable() {

        Integer integer = book.updateBook(new Book());
        System.out.println("是否进行了事务回退"+integer);
        bookService.testTry();
        System.out.println("更新成功。。。。。。。。。。。。。。。。");
    }
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    @Retryable(value = {DBVersionException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
    public void testTry(){
//        Integer version =book.queryOrderVersion(Arrays.asList(1L));
//        Integer count=book.updateOrder(version);
//        if(!Objects.equals(count,1)){
//            System.out.println("重试中。。。。。。。。。。。。。。。。");
//            throw new DBVersionException();
//        }
    }
}
